package com.hexl.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hexl.common.utils.security.ShiroUtils;
import com.hexl.framework.web.domain.BaseEntity;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @author hexinglin
 * mybatisplus 自动填充创建时间，创建人，修改时间，修改人
 * @created 2021-07-09 16:17
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if (Objects.isNull(this.getFieldValByName("createBy",metaObject))){
            this.setFieldValByName("createBy", Objects.nonNull(ShiroUtils.getLoginName())?ShiroUtils.getLoginName():"0000",metaObject);
        }
        if (Objects.isNull(this.getFieldValByName("createTime",metaObject))){
            this.setFieldValByName("createTime",new Date(),metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (Objects.isNull(this.getFieldValByName("updateBy",metaObject))){
            this.setFieldValByName("updateBy", Objects.nonNull(ShiroUtils.getLoginName())?ShiroUtils.getLoginName():"0000",metaObject);
        }
        if (Objects.isNull(this.getFieldValByName("updateTime",metaObject))){
            this.setFieldValByName("updateTime",new Date(),metaObject);
        }
    }
}

